Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move common pieces of mock REST servers to a new package #267

Merged
merged 15 commits into from
Sep 12, 2023

Conversation

CarlosNihelton
Copy link
Contributor

The store mock server is structurally very close to the contracts mock: YAML configuration, similar CLI, both talk via REST, both may have blocked or disabled endpoints etc.

Thus I though that I could move the guts of the contracts mock into a new package and make both mock servers inherit from this common base.

No semantics or endpoint behaviors should have changed with this.

Package restserver implements the building blocks to create similar mock
servers that talk REST.
Application type holds some global config and the cobra Cmds.
ServerBase contains most of what a Server implementation will need.
It requires:
 - a function that provides the net address
 - a configured ServeMux

 It offers:

 - serve
 - stop
 - validation boilerplate (the method was previously called `handle`,
   now `ValidateRequest` :) )

 Type-erasing `Settings` (via interface) was necessary, because
 individual servers will have their own needs.

 (PS: I'm keen to ideas of how to remove the dependency on GetAddress
 and SetAddress)
I managed to sneak a little bit of OOP into Go :)

contractsmockserver main business now is its own initialization
which requires creating a ServeMux and handling the requests that manage
to pass through validation.

Boilerplate was "inherited" from ServerBase.
Much shorter :)
@CarlosNihelton CarlosNihelton marked this pull request as ready for review September 8, 2023 06:59
@CarlosNihelton
Copy link
Contributor Author

#268 shows how close those servers are :)

Copy link
Contributor

@EduardGomezEscandell EduardGomezEscandell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very, very nice stuff. Really cool how small the server specialization ends up being, both here and in #268.

Maybe we could simplify some things with generics to avoid the factories, but the effort may not be worth the savings.

mocks/restserver/application.go Outdated Show resolved Hide resolved
mocks/restserver/application.go Outdated Show resolved Hide resolved
mocks/restserver/application.go Outdated Show resolved Hide resolved
mocks/restserver/restserver.go Outdated Show resolved Hide resolved
mocks/restserver/restserver.go Outdated Show resolved Hide resolved
mocks/restserver/restserver.go Outdated Show resolved Hide resolved
mocks/restserver/application.go Outdated Show resolved Hide resolved
mocks/contractserver/contracts/main.go Outdated Show resolved Hide resolved
mocks/restserver/application.go Outdated Show resolved Hide resolved
and the receiver becomes "app" instead of simply "a"
That's the only place where that method is useful.
Address can only be set via Serve()
Serve() accepts empty string for address, defaulting to "localhost:0"
Initialization of Server implementations become cleaner
No need to leak the address getters/setters to other levels.
All handled within the restserver package.
Copy link
Contributor

@EduardGomezEscandell EduardGomezEscandell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes, this is looking nice.

mocks/contractserver/contracts/main.go Show resolved Hide resolved
mocks/contractserver/contracts/main.go Outdated Show resolved Hide resolved
mocks/restserver/restserver.go Show resolved Hide resolved
mocks/restserver/application.go Outdated Show resolved Hide resolved
Copy link
Contributor

@EduardGomezEscandell EduardGomezEscandell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@CarlosNihelton CarlosNihelton merged commit 8ac5850 into main Sep 12, 2023
@CarlosNihelton CarlosNihelton deleted the rest-servers branch September 12, 2023 10:52
CarlosNihelton added a commit that referenced this pull request Sep 15, 2023
The details of the responses may change soon, but the most important
thing about this PR is that it puts in place the code we need to run a
REST server that will mock the MS Store API.

It reuses the bits extracted from the contracts server mock in #267 and
reimplements the relevant parts to create the following endpoints:

`/allauthenticatedusers` - returns some representation of the user
accounts found locally authenticated on the system.
`/generateuserjwt` - returns the user JWT if the query parameters check
and the settings allow.
`/getproducts` - returns a collection of products - JSON content type.
`/purchase` - handles a purchase request, updating the in-memory server
state if the transaction is accepted.

That should be enough for us to implement the client API and some test
cases & fixtures soon.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants